package com.lanou.horo.zhuizhui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.entity.EveryDayItem;
import com.lanou.horo.zhuizhui.utility.clickInterface.EveryDayClick;
import com.lanou.horo.zhuizhui.utility.imageLoader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class EveryDayAdapter extends RecyclerView.Adapter<EveryDayAdapter.EveryDayViewHolder> {
    private List<EveryDayItem.ThemeListEntity> entities;
    private ImageLoader imageLoader;
    private Context context;
    private EveryDayClick everyDayClick;

    public void setEveryDayClick(EveryDayClick click) {
        this.everyDayClick = click;
    }

    public EveryDayAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addList(List<EveryDayItem.ThemeListEntity> datas) {
        entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public EveryDayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_day_every_item, null);
        return new EveryDayViewHolder(view);
    }

    @Override
    public void onBindViewHolder(EveryDayViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            EveryDayItem.ThemeListEntity entity = entities.get(position);
            if (entity != null) {
                String url = entity.getCover();
                holder.everyDayimage.setImageUrl(url, imageLoader);
                holder.everyDayContent.setText(entity.getName());
                holder.itemPosition = position;
            }
        }
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class EveryDayViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView everyDayimage;
        private TextView everyDayContent;
        private int itemPosition;

        public EveryDayViewHolder(View itemView) {
            super(itemView);
            everyDayContent = (TextView) itemView.findViewById(R.id.everyDayContent);
            everyDayimage = (NetworkImageView) itemView.findViewById(R.id.everyDayimage);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (entities != null) {
                EveryDayItem.ThemeListEntity et = entities.get(itemPosition);
                if (everyDayClick != null) {
                    everyDayClick.everyDayItemClick(et);
                }
            }
        }
    }
}
